package com.ruoyi.tb.lmt.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Arrays;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.helper.LoginHelper;
import com.ruoyi.tb.lmt.domain.bo.TbAcceptanceBo;
import com.ruoyi.tb.lmt.domain.bo.TbSpecialInspectionBo;
import com.ruoyi.tb.lmt.domain.bo.TbSpecialInspectiondetailBo;
import com.ruoyi.tb.lmt.domain.vo.AllMuSpeAccTaskVo;
import com.ruoyi.tb.lmt.domain.vo.TbSpecialInspectiondetailVo;
import com.ruoyi.tb.lmt.service.ITbAcceptanceService;
import lombok.RequiredArgsConstructor;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;

import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.tb.lmt.domain.vo.TbAcceptancedetailVo;
import com.ruoyi.tb.lmt.domain.bo.TbAcceptancedetailBo;
import com.ruoyi.tb.lmt.service.ITbAcceptancedetailService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 验收详情
 *
 * @author ruoyi
 * @date 2024-01-11
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/xha-lmt/acceptancedetail")
public class TbAcceptancedetailController extends BaseController {

    private final ITbAcceptancedetailService iTbAcceptancedetailService;

    @Autowired
    private ITbAcceptanceService iTbAcceptanceService;

    /**
     * 查询验收详情列表
     */
    @GetMapping("/list")
    public TableDataInfo<TbAcceptancedetailVo> list(TbAcceptancedetailBo bo, PageQuery pageQuery) {
        return iTbAcceptancedetailService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出验收详情列表
     */
    @Log(title = "验收详情", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(TbAcceptancedetailBo bo, HttpServletResponse response) {
        List<TbAcceptancedetailVo> list = iTbAcceptancedetailService.queryList(bo);
        ExcelUtil.exportExcel(list, "验收详情", TbAcceptancedetailVo.class, response);
    }

    /**
     * 获取验收详情详细信息
     *
     * @param ID 主键
     */
    @SaCheckPermission("xha-lmt:acceptancedetail:query")
    @GetMapping("/{ID}")
    public R<TbAcceptancedetailVo> getInfo(@NotNull(message = "主键不能为空")
                                           @PathVariable Long ID) {
        return R.ok(iTbAcceptancedetailService.queryById(ID));
    }

    /**
     * 新增验收详情
     */
    @SaCheckPermission("xha-lmt:acceptancedetail:add")
    @Log(title = "验收详情", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody TbAcceptancedetailBo bo) {
        return toAjax(iTbAcceptancedetailService.insertByBo(bo));
    }

    /**
     * 修改验收详情
     */
    @SaCheckPermission("xha-lmt:acceptancedetail:edit")
    @Log(title = "验收详情", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody TbAcceptancedetailBo bo) {
        return toAjax(iTbAcceptancedetailService.updateByBo(bo));
    }

    /**
     * 删除验收详情
     *
     * @param IDs 主键串
     */
    @SaCheckPermission("xha-lmt:acceptancedetail:remove")
    @Log(title = "验收详情", businessType = BusinessType.DELETE)
    @DeleteMapping("/{IDs}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] IDs) {
        return toAjax(iTbAcceptancedetailService.deleteWithValidByIds(Arrays.asList(IDs), true));
    }

    @SaCheckPermission("xha-lmt:acceptancedetail:addAcceptance")
    @Log(title = "新增验收详情任务", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping("/addAcceptance")
    public R<Void> addAcceptance(@Validated(AddGroup.class) @RequestBody Map map) {
        try {
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            TbAcceptanceBo tbAcBo = new TbAcceptanceBo();
            tbAcBo.setId(map.get("mutualId") == null ? null : Long.parseLong((String) map.get("mutualId")));
            String requireStartTime = (String) map.get("repairStartDate");
            if (requireStartTime != null && !requireStartTime.isEmpty()) {
                tbAcBo.setRepairStartDate(formatter.parse(requireStartTime));
            }
            iTbAcceptanceService.updateByBo(tbAcBo);

            TbAcceptancedetailBo tbAcceptBo = new TbAcceptancedetailBo();
            tbAcceptBo.setMutualId(map.get("mutualId") == null ? null : Long.parseLong((String) map.get("mutualId")));
            tbAcceptBo.setWorkName((String) map.get("workName"));
            tbAcceptBo.setWorkKind((String) map.get("workKind"));
            String workGroupIdString = (String) map.get("workCategoryId");
            if (workGroupIdString != null && !workGroupIdString.isEmpty()) {
                try {
                    Long workGroupId = Long.parseLong(workGroupIdString);
                    tbAcceptBo.setWorkCategoryId(workGroupId);
                } catch (NumberFormatException e) {
                    e.printStackTrace();
                }
            }
            tbAcceptBo.setWorkCategory((String) map.get("workCategory"));
            tbAcceptBo.setWorkCategoryRemark((String) map.get("workCategoryRemark"));
            tbAcceptBo.setStatus("未提报");
            tbAcceptBo.setCheckReapirReqire((String) map.get("checkReapirReqire"));
            tbAcceptBo.setTaskUrl((String) map.get("taskUrl"));
            tbAcceptBo.setCreateTime(new Date());
            LoginUser currentUser = LoginHelper.getLoginUser();
            tbAcceptBo.setCreateBy(currentUser.getUsername());
            iTbAcceptancedetailService.insertByBo(tbAcceptBo);
            return R.ok();
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
    }

    @SaCheckPermission("xha-lmt:acceptancedetail:updateAcceptance")
    @Log(title = "修改验收任务", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping("/updateAcceptance")
    public R<Void> updateAcceptance(@Validated(AddGroup.class) @RequestBody Map map) {
        try {
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            TbAcceptanceBo tbAcBo = new TbAcceptanceBo();
            tbAcBo.setId(map.get("mutualId") == null ? null : Long.parseLong((String) map.get("mutualId")));
            tbAcBo.setRepairStartDate(map.get("repairStartDate") == null ? null : formatter.parse((String) map.get("repairStartDate")));
            iTbAcceptanceService.updateByBo(tbAcBo);
            TbAcceptancedetailBo tbAcceptBo = new TbAcceptancedetailBo();
            LoginUser currentUser = LoginHelper.getLoginUser();
            tbAcceptBo.setID(map.get("id") == null ? null : Long.parseLong((String) map.get("id")));
            tbAcceptBo.setMutualId(map.get("mutualId") == null ? null : Long.parseLong((String) map.get("mutualId")));
            tbAcceptBo.setWorkName((String) map.get("workName"));
            tbAcceptBo.setWorkKind((String) map.get("workKind"));
            tbAcceptBo.setWorkCategoryId(map.get("workCategoryId") == null ? null : Long.parseLong((String) map.get("workCategoryId")));
            tbAcceptBo.setWorkCategory((String) map.get("workCategory"));
            tbAcceptBo.setWorkCategoryRemark((String) map.get("workCategoryRemark"));
            tbAcceptBo.setCheckReapirReqire((String) map.get("checkReapirReqire"));
            tbAcceptBo.setRemark((String) map.get("remark"));
            tbAcceptBo.setWorkGroup((String) map.get("workGroup"));
            tbAcceptBo.setWorkUser((String) map.get("workUser"));
            tbAcceptBo.setWorkUser2((String) map.get("workUser2"));
            tbAcceptBo.setTaskUrl((String) map.get("taskUrl"));
            tbAcceptBo.setUpdateTime(new Date());
            tbAcceptBo.setUpdateBy(currentUser.getUsername());

            iTbAcceptancedetailService.updateByBo(tbAcceptBo);
            return R.ok();
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
    }

    //查询验收任务
    @GetMapping("/list/selectAcceptance")
    public TableDataInfo<TbAcceptancedetailVo> selectAcceptance(Long mutualId, String lmtType, String lmtNumber, String status, PageQuery pageQuery) {
        return iTbAcceptancedetailService.selectAcceptance(mutualId, lmtType, lmtNumber, status, pageQuery);
    }

    //查询全部问题任务
    @GetMapping("/list/selectAllTask")
    public TableDataInfo<AllMuSpeAccTaskVo> selectAllTask(String lmtType, String lmtNumber, String status, PageQuery pageQuery) {
        return iTbAcceptancedetailService.selectAllTask(lmtType, lmtNumber, status, pageQuery);
    }
}
